package com.centaline.androidsalesblog.fragments.list;

import android.content.Intent;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.newest.NewEstDetailActivity;
import com.centaline.androidsalesblog.adapter.NewEstAdapter;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.databaseobject.BizUnitDo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.trello.rxlifecycle.FragmentEvent;

import java.util.ArrayList;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 新盘列表
 * Created by guilin on 16/2/23.
 */
public class NewEstListFragment extends BaseFragment {

    private MRecyclerView mRecyclerView;
    private NewEstAdapter newEstAdapter;
    private ArrayList<NewEstDo> list = new ArrayList<>();
    private int iRefreshType = 1;
    private RefreshListener refreshListener;
    private IRecycleViewListener iRecycleViewListener = new IRecycleViewListener() {
        @Override
        public void downRefresh() {
            iRefreshType = 1;
            refreshListener.down();
        }

        @Override
        public void upRefresh() {
            iRefreshType = 2;
            refreshListener.up(list.size());
        }

        @Override
        public void onItemClick(int position) {
            startActivity(new Intent(getActivity(), NewEstDetailActivity.class)
                    .putExtra(ExtraConstant.NewEstId, list.get(position).getEstExtId()));
        }
    };

    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.layout_m_recyclerview;
    }

    @Override
    protected void findViews(View view) {
        mRecyclerView = (MRecyclerView) view.findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initView() {
        mRecyclerView.setIRecycleViewListener(iRecycleViewListener);
        newEstAdapter = new NewEstAdapter(getActivity(), GlideLoad.init(this), list);
        mRecyclerView.setAdapter(newEstAdapter);
        bizUnit();
        startRefresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 开启下拉刷新
     */
    public void startRefresh() {
        mRecyclerView.startRefresh();
    }

    /**
     * 设置定位信息
     */
    public void setLocation(LatLng latLng) {
        newEstAdapter.setLocLatLng(latLng);
    }

    /**
     * 停止刷新
     */
    public void stopRefresh(ArrayList<NewEstDo> arrayList) {
        if (iRefreshType == 1) {
            list.clear();
        }
        if (arrayList == null) {
            mRecyclerView.stopRefresh(false);
        } else {
            list.addAll(arrayList);
            mRecyclerView.stopRefresh(arrayList.size() == 10);
        }
    }

    public void refreshError() {
        mRecyclerView.refreshError();
    }

    private void bizUnit() {
        RxDataBase.getBizUnit()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(this.<BizUnitDo>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<BizUnitDo>() {
                    @Override
                    public void call(BizUnitDo bizUnitDo) {
                        newEstAdapter.setNewPropImg(bizUnitDo.getNewpropimg());
                    }
                });
    }
}
